<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    top="8vh"
    @close="handleClose"
    @open="handleOpen"
  >
    <!-- 表单 -->
    <el-form ref="addForm" :model="employeeForm" :rules="hadleadd" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeForm.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employeeForm.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="employeeForm.timeOfEntry"
          style="width:50%"
          placeholder="请选择入职时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeForm.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in hireTyprs"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeForm.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!--
          readonlty : 只读
          disabled ： 禁用
         -->
        <el-input
          v-model="employeeForm.departmentName"
          readonly
          style="width:50%"
          placeholder="请选择部门"
          @focus="isShow = true"
        />
        <el-tree
          v-if="isShow"
          :data="deptTreeDate"
          :props="{label:'name'}"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="employeeForm.correctionTime"
          style="width:50%"
          placeholder="请选择转正时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入常量模块
import obj from '@/constant/employees'
import { addEmployeeAPI } from '@/api/employees'
import { getDeptListAPI } from '@/api/departments'
import { list2Tree } from '@/utils/departments'

export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 聘用形式列表
      hireTyprs: obj.hireType,
      // 部门树形解构数据
      deptTreeDate: [],
      // 控制树形组件的显示
      isShow: false,
      // 员工表单
      employeeForm: {
        username: '', // 员工名字
        mobile: '', // 员工手机号
        timeOfEntry: '', // 员工入职日期
        formOfEmployment: 1, // 员工聘用形式
        workNumber: '', // 员工工号
        departmentName: '', // 员工部门名字
        correctionTime: '' // 员工转正日期
      },
      // 校验规则
      hadleadd: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 6, message: '用户姓名1-6位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入入职时间', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        correctionTime: [
          { required: true, message: '请输入入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹框关闭时
    handleClose() {
      this.$refs.addForm.resetFields()
      // 重置表单
      this.$emit('update:showDialog', false)
    },
    // 弹框打开时
    async handleOpen() {
      // 发起获取部门列表请求
      const resp = await getDeptListAPI()
      // 转为树形结构在赋值
      this.deptTreeDate = list2Tree(resp.data.depts, '')
    },
    // 树形节点点击时
    handleNodeClick(node) {
      // node : 被点击的部门(节点) 对象
      // 如果有子部门，直接结束
      if (node.children) return
      // 否则赋值当前选择的部门
      this.employeeForm.departmentName = node.name
      // 隐藏树形结构
      this.isShow = false
    },
    // 确定提交数据
    async handleAdd() {
      try {
        // 整体校验
        this.$refs.addForm.validate(async item => {
          if (!item) return
          // 发起新增请求
          await addEmployeeAPI(this.employeeForm)
          // 关闭弹框
          this.handleClose()
          // 获取员工列表
          // this.$parent.getEmployeeList()

          // 推荐子传父
          this.$emit('add-success')
          // 给个成功的提示
          this.$message.success('添加成功')
        })
      } catch (err) {
        // 给个失败的提示
        this.$message.info('添加失败')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
